package logic

import (
	"context"
	"time"

	"backend/internal/svc"
	"backend/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetFundListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetFundListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFundListLogic {
	return &GetFundListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetFundListLogic) GetFundList(req *types.FundListReq) (resp *types.FundListResp, err error) {
	// Mock data for demonstration
	funds := []types.Fund{
		{
			Id:        "1",
			Name:      "易方达中小盘混合",
			Code:      "110011",
			Type:      "混合型",
			Nav:       5.423,
			UpdatedAt: time.Now().Format("2006-01-02"),
		},
		{
			Id:        "2",
			Name:      "广发创新升级混合",
			Code:      "002939",
			Type:      "混合型",
			Nav:       2.876,
			UpdatedAt: time.Now().Format("2006-01-02"),
		},
		{
			Id:        "3",
			Name:      "富国天惠成长混合(LOF)",
			Code:      "161005",
			Type:      "混合型",
			Nav:       3.125,
			UpdatedAt: time.Now().Format("2006-01-02"),
		},
	}

	// Filter by type if provided
	if req.Type != "" {
		var filtered []types.Fund
		for _, fund := range funds {
			if fund.Type == req.Type {
				filtered = append(filtered, fund)
			}
		}
		funds = filtered
	}

	// Pagination logic (simplified)
	start := (req.Page - 1) * req.PageSize
	end := start + req.PageSize
	if start >= int64(len(funds)) {
		return &types.FundListResp{
			Funds: []types.Fund{},
			Total: int64(len(funds)),
		}, nil
	}
	if end > int64(len(funds)) {
		end = int64(len(funds))
	}

	return &types.FundListResp{
		Funds: funds[start:end],
		Total: int64(len(funds)),
	}, nil
}

type GetFundDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetFundDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFundDetailLogic {
	return &GetFundDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetFundDetailLogic) GetFundDetail(req *types.FundDetailReq) (resp *types.FundDetailResp, err error) {
	// Mock data for demonstration
	fund := types.Fund{
		Id:        req.Id,
		Name:      "易方达中小盘混合",
		Code:      "110011",
		Type:      "混合型",
		Nav:       5.423,
		UpdatedAt: time.Now().Format("2006-01-02"),
	}

	return &types.FundDetailResp{
		Fund: fund,
	}, nil
}

type GetFundPerformanceLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetFundPerformanceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFundPerformanceLogic {
	return &GetFundPerformanceLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetFundPerformanceLogic) GetFundPerformance(req *types.FundPerformanceReq) (resp *types.FundPerformanceResp, err error) {
	// Mock data for demonstration
	performances := []types.FundPerformance{
		{
			Id:        "1",
			FundId:    req.FundId,
			Date:      "2023-01-01",
			Return:    0.0123,
			Benchmark: 0.0056,
		},
		{
			Id:        "2",
			FundId:    req.FundId,
			Date:      "2023-01-02",
			Return:    -0.0045,
			Benchmark: -0.0078,
		},
		{
			Id:        "3",
			FundId:    req.FundId,
			Date:      "2023-01-03",
			Return:    0.0234,
			Benchmark: 0.0189,
		},
	}

	return &types.FundPerformanceResp{
		Performances: performances,
	}, nil
}
